//
//  WMEFirstPageCollectionViewCell.m
//  WMEMusic
//
//  Created by 恩叔 on 16/4/12.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEFirstPageCollectionViewCell.h"
#import "WMEOneInformationCollectionViewCell.h"
#import "WMEFirstPageModel.h"
@interface WMEFirstPageCollectionViewCell ()<UICollectionViewDataSource ,UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSNumber *haveMore;
@property (nonatomic, assign)NSInteger nextUrl;
@end
@implementation WMEFirstPageCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        WS(ws);
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((WIDTH - 40.1) / 3, (WIDTH - 40) / 3 * 1.25);
        layout.minimumInteritemSpacing = 10;
        layout.minimumLineSpacing = 10;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 94) collectionViewLayout:layout];
        [self.contentView addSubview:_collectionView];
        _collectionView.backgroundColor = [UIColor lightGrayColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(ws.mas_left).with.offset(0);
            make.right.equalTo(ws.mas_right).with.offset(0);
            make.top.equalTo(ws.mas_top).with.offset(0);
            make.bottom.equalTo(ws.mas_bottom).with.offset(0);
        }];
        [_collectionView registerClass:[WMEOneInformationCollectionViewCell class] forCellWithReuseIdentifier:@"oneInformationCell"];
        self.nextUrl = 1;
        [self data];
        [self loadFunction];
        [self refreshFunction];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArr.count) {
        WMEOneInformationCollectionViewCell *informationCell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"oneInformationCell" forIndexPath:indexPath];
        WMEFirstPageModel *model = _dataArr[indexPath.row];
        informationCell.model = model;
        return informationCell;
    }
    return nil;
}
- (void)data
{
    NSString *urlStr = @"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.diy.gedan&page_no=1&page_size=30&from=ios&version=5.5.6&from=ios&channel=appstore&operator=1";
    [WMEAFNetworking GETNetWithUrl:urlStr body:nil headerFile:nil response:WMEJOSN success:^(id result) {
        NSArray *temp = result[@"content"];
        self.haveMore = result[@"havemore"];
        self.dataArr = [WMEFirstPageModel modelHandlerWithArray:temp];
        [_collectionView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"网络请求错误");
    }];
}
- (void)refreshFunction
{
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArr removeAllObjects];
        [self data];
        [self.collectionView.mj_header endRefreshing];
    }];
}
- (void)loadFunction
{
    if (_haveMore.integerValue != 1) {
        
        self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.nextUrl += 1;
            NSString *tempStr = @"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.diy.gedan&page_no=1&page_size=30&from=ios&version=5.5.6&from=ios&channel=appstore&operator=1";
            NSString *urlStr = [tempStr stringByReplacingOccurrencesOfString:@"page_no=1" withString:[NSString stringWithFormat:@"page_no=%ld", _nextUrl]];
            [WMEAFNetworking GETNetWithUrl:urlStr body:nil headerFile:nil response:WMEJOSN success:^(id result) {
                NSArray *tempArr = result[@"content"];
                NSArray *arr = [WMEFirstPageModel modelHandlerWithArray:tempArr];
                [_dataArr addObjectsFromArray:arr];
                [_collectionView reloadData];
                [_collectionView.mj_footer endRefreshing];
                
            } failure:^(NSError *error) {
                NSLog(@"网络请求错误");
            }];
        }];
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WMEFirstPageModel *model = _dataArr[indexPath.row];
//    NSString *str = [@"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.diy.gedanInfo&from=ios&listid=6464&version=5.5.6&from=ios&channel=appstore&operator=1" stringByReplacingOccurrencesOfString:@"listid=6464" withString:[NSString stringWithFormat:@"listid=%@",model.listid]];
    self.firstBlock(model);
}


@end
